<?php
/*
 * Created on Jan 25, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 * 
 * 
 */
 
include_once("../configure/configure.php");		//--> global var
include_once("../lib/impl/MerchantImpl.php");
include_once("../lib/impl/TagImpl.php");
include_once("../lib/impl/CategoryImpl.php");
include_once("../lib/impl/CouponImpl.php");
include_once("../lib/utils/AlgorithmUtils.php");

set_time_limit(0);

calUseImage();

calMerNameFirstLetter();
calMerchantCouponCount();

calIndexCouponCount();

calTagCouponCount();
calCategoryCouponCount();

calFeaturedCoupon();

expireCoupon();
//deleteExpireCoupon();


function calFeaturedCoupon()
{
	$algorithm 		= new AlgorithmUtils();
	$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	
	$oMerchants 	= $oMerchantImpl->getValidAllMerchants();
	
	foreach($oMerchants as $oMerchant){
		$oCoupons 		= $oCouponImpl->getCouponsByMerchant($oMerchant->getId(), 0, 10000);		
		$arrSortCoupons = $algorithm->getFeaturedCoupon($oCoupons);
		
		//set all coupon featured invalid for this merchant.
		$oCouponImpl->setFeaturedByMerchantId($oMerchant->getId());
		if(count($arrSortCoupons) > 0){
			$oTopCoupon = array_pop($arrSortCoupons);
			
			//set first coupon to featured 
			$oCouponImpl->setFeatured($oTopCoupon->getId(), $oTopCoupon->getRsortValue());
		}
	}
}



function calMerNameFirstLetter()
{
	$count = 0;
	
	$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);

	$oMerchants 	= $oMerchantImpl->getValidAllMerchants(DB_TAG_COUPON);
	
	foreach($oMerchants as $oMerchant){
		$merId 		= $oMerchant->getId();
		
		$merName 	= $oMerchant->getName();
		$merName 	= trim($merName);
		
		$firstCharValue = 0;
		
		$firstChar 		= strtolower($merName[0]);
		
		if(ereg('^[a-zA-Z]+$', $firstChar)){
			$sql = "UPDATE merchant SET r_nameFirstLetter = '$firstChar' WHERE id = $merId";
			
		}else{
			$sql = "UPDATE merchant SET r_nameFirstLetter = '#' WHERE id = $merId";
		}
		
		$count 	+= $oMerchantImpl->update(DB_TAG_COUPON, $sql);
	}
	
	echo 'update first letter merchant count = ' . $count . "<br>";
}

function calTagCouponCount()
{
	$count = 0;
	
	$oTagImpl 	= new TagImpl(DB_TAG_COUPON);

	$arrTagCouponCount 	= $oTagImpl->getTagCouponCount(DB_TAG_COUPON);
	
	foreach($arrTagCouponCount as $tagId => $couponCount){
		$sql = "UPDATE tag SET r_couponCount = $couponCount WHERE id = $tagId";
		
		$count 	+= $oTagImpl->update(DB_TAG_COUPON, $sql);
	}
	
	echo 'update tag valid coupon count = ' . $count . "<br>";
	
	$count = 0;
	
	$arrHasCouponTags 	= $oTagImpl->getHasCouponTags(DB_TAG_COUPON);
	$invalidCouponCount = array_diff_key ($arrHasCouponTags, $arrTagCouponCount);
	
	foreach($invalidCouponCount as $tagId => $couponCount){
		$sql = "UPDATE tag SET r_couponCount = 0 WHERE id = $tagId";
		
		$count 	+= $oTagImpl->update(DB_TAG_COUPON, $sql);
	}
	
	echo 'update tag invalid coupon count = ' . $count . "<br>";
}

function calCategoryCouponCount()
{
	$count = 0;
	
	$oCategoryImpl	= new CategoryImpl(DB_TAG_COUPON);
	
	$arrCouponCount = $oCategoryImpl->getCouponCount(DB_TAG_COUPON);
	
	foreach($arrCouponCount as $catId => $couponCount){
		if($catId > 0){
			$sql	= "UPDATE category SET r_CouponCount = $couponCount  WHERE id = $catId";
			$count 	+= $oCategoryImpl->update(DB_TAG_COUPON, $sql);
		}
	
	}
	
	echo 'update valid category coupon count = ' . $count . "<br>";
	
	$count = 0;
	
	$arrHasCoupons 		= $oCategoryImpl->getHasCoupons(DB_TAG_COUPON);
	$invalidCouponCount = array_diff_key ($arrHasCoupons, $arrCouponCount);
	
	foreach($invalidCouponCount as $merId => $couponCount){
		$sql = "UPDATE category SET r_couponCount = 0 WHERE id = $merId";
		
		$count 	+= $oCategoryImpl->update(DB_TAG_COUPON, $sql);
	}
	
	
	$arrCategories = $oCategoryImpl->getCustoms('', '', 0, 1000);
			
	foreach($arrCategories as $oCat){
		$couponCount = $oCategoryImpl->getCategoryCouponCount($oCat->getId());
		
		$sql = "UPDATE category SET r_couponCount = $couponCount WHERE id = " . $oCat->getId();
		
		$oCategoryImpl->update(DB_TAG_COUPON, $sql);
	}
}


function calMerchantCouponCount()
{
	$count = 0;
	
	$oMerchantImpl	= new MerchantImpl(DB_TAG_COUPON);
	
	$arrMerIdCouponCount = $oMerchantImpl->getCouponCount(DB_TAG_COUPON);
	
	foreach($arrMerIdCouponCount as $merId => $couponCount){
		if($merId > 0){
			$sql	= "UPDATE merchant SET r_CouponCount = $couponCount  WHERE id = $merId";
			$count 	+= $oMerchantImpl->update(DB_TAG_COUPON, $sql);
		}
	
	}
	
	echo 'update valid merchant coupon count = ' . $count . "<br>";
	
	$count = 0;
	
	$arrHasCoupons 		= $oMerchantImpl->getHasCoupons();
	$invalidCouponCount = array_diff_key ($arrHasCoupons, $arrMerIdCouponCount);
	
	foreach($invalidCouponCount as $merId => $couponCount){
		$sql = "UPDATE merchant SET r_couponCount = 0 WHERE id = $merId";
		
		$count 	+= $oMerchantImpl->update(DB_TAG_COUPON, $sql);
	}
	
	echo 'update invalid merchant coupon count = ' . $count . "<br>";
}

function calIndexCouponCount()
{
	$count = 0;
	
	$oTagImpl 	= new TagImpl(DB_TAG_COUPON);
	
	$arrTags = $oTagImpl->getCustoms(" WHERE status = 1", '', 0, 200000);
	
	echo 'tag count = ' . count($arrTags) . "<br>";
	
	foreach($arrTags as $oTag){
		$keywordId = $oTagImpl->getColumn(DB_TAG_COUPON, "SELECT id FROM keywords_coupon WHERE keywords = '" . addslashes($oTag->getName()) . "'");

		if(!empty($keywordId)){
//			echo 'keyword id = ' . $keywordId . "<br>";
			
			$sql 		= "SELECT couponId FROM keywords_coupon_mapping WHERE keywordId = $keywordId";
			$allData 	= $oTagImpl->getAllData(DB_TAG_COUPON, $sql);
			
//			echo 'count count = ' . count($allData) . "<br>";
			foreach($allData as $data){
				$sql_data_array = array(
					'couponId' 	=> $data['couponId'],
					'tagId' => $oTag->getId(),
					'authorized' => '1'
				);
					
				$count += $oTagImpl->insertUpdate(DB_TAG_COUPON, 'coupon_tag', $sql_data_array, 'insert', " ON DUPLICATE KEY UPDATE authorized=Values(authorized), modifiedTime=Values(modifiedTime)");
				
			}
		}
	}
	
	echo 'update coupon tag count = ' . $count . "<br>";
}


function expireCoupon()
{
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	
	$sql	= "UPDATE coupon SET status = 2 WHERE status = 1 AND endDate < curdate() AND endDate != '0000-00-00'";
	$count 	= $oCouponImpl->update(DB_TAG_COUPON, $sql);
	
	echo 'update expire coupon count = ' . $count . "<br>";
	

}

function deleteExpireCoupon()
{
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	
	$sql	= "DELETE FROM coupon WHERE status = 2 AND endDate < (CURDATE() - 30)";
	$count 	= $oCouponImpl->update(DB_TAG_COUPON, $sql);
	
	echo 'delete expire coupon count = ' . $count . "<br>";
	

}

function calFeaturedCouponForTest()
{
	$algorithm 		= new AlgorithmUtils();
	$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	
	$oMerchants = $oMerchantImpl->getValidAllMerchants();
	
	$tmp = 0;
	foreach($oMerchants as $oMerchant){
		$attributes = $oMerchantImpl->getAttributes($oMerchant->getId());
		
		$oCoupons 	= $oCouponImpl->getCouponsByMerchant($oMerchant->getId(), 0, 50);
		
		$arrSortCoupons = $algorithm->getFeaturedCoupon($oCoupons);
		
		$merchantWeight = 0;
		if(is_array($attributes) && count($attributes) > 0){
			if(array_key_exists("19", $attributes)){
				$merchantWeight = 0;
			}
		}
		
		//set first top coupon
		$oCouponImpl->setFeaturedByMerchantId($oMerchant->getId());
		if(count($arrSortCoupons) > 0){
			$oTopCoupon = array_pop($arrSortCoupons);
			$oCouponImpl->setFeatured($oTopCoupon->getId());
		}
		
		
		echo "**********************************************************************8<br>";
		foreach($arrSortCoupons as $key=>$oCoupon){
			echo "=============================================<br>";
			echo "coupon id: " . $oCoupon->getId() . "<br>";
			echo "coupon key: " . $key . "<br>";
			echo "coupon code: " . $oCoupon->getCode() . "<br>";
			echo "coupon name: " . $oCoupon->getName() . "<br>";
			echo "coupon description: " . $oCoupon->getDescript() . "<br>";
			
			
		}
		
		$tmp++;
		
		if($tmp > 30)
			break;
	}
}

function calUseImage()
{
	$oCommon 	= new Common();

	
	$sql		= "SELECT id FROM coupon WHERE creativeType = 1 AND name = 'logo'";
	$allData 	= $oCommon->getAllData(DB_TAG_COUPON, $sql);
	foreach($allData as $arrRow){
		$sql	= "UPDATE coupon SET status = 0 WHERE id = " . $arrRow['id'];
		$oCommon->update(DB_TAG_COUPON, $sql);
		
		echo 'logo data id = ' . $arrRow['id'] . '<br>';
	}

	
	$sql		= "SELECT id FROM coupon WHERE creativeType = 1 AND status in (1, 2)";
	$allData 	= $oCommon->getAllData(DB_TAG_COUPON, $sql);
	
	echo 'waitting update calculate count = ' . count($allData) . '<br>';
	foreach($allData as $arrRow){
		$sql		= "SELECT id, width, height FROM coupon_images WHERE status = 1 AND couponId = " . $arrRow['id'];
		$sql		.= " AND width < 401 AND height < 401 AND (useType = 0 OR useType = 1)";
		
		$images 	= $oCommon->getAllData(DB_TAG_COUPON, $sql);
		
//		echo 'images count = ' . count($images) . '<br>';
		
		$arrImages = array();
		foreach($images as $rows){
			$maxWH 		= max($rows['width'], $rows['height']);
			
			$imageId	= $rows['id'];
			
			$arrImages[$maxWH] = $imageId;
		}
		
		if(count($arrImages) > 0){
			krsort($arrImages);
		
			$imageId = array_shift($arrImages);
			
			$sql	= "UPDATE coupon_images SET useType = 0 WHERE couponId = " . $arrRow['id'];
			$oCommon->update(DB_TAG_COUPON, $sql);

			$sql	= "UPDATE coupon_images SET useType = 1 WHERE id = $imageId";
			$oCommon->update(DB_TAG_COUPON, $sql);
			
			$sql	= "UPDATE coupon SET status = 1 WHERE id = " . $arrRow['id'];
			$oCommon->update(DB_TAG_COUPON, $sql);
		}else{
			// not image status
			$sql	= "UPDATE coupon SET status = 2 WHERE id = " . $arrRow['id'];
			$oCommon->update(DB_TAG_COUPON, $sql);
		}
	}
}
?>
